package com.winexpress.session.order;

import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import com.winexpress.entity.Wine;
import com.winexpress.entity.Sales;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.persistence.Query;

@Stateless
@LocalBean
public class SalesSessionBean {

    @PersistenceContext
    private EntityManager em;

    public List<Wine> getTopSellers() {
        int limit = 3;
        String strQuery = "SELECT s FROM Sales s WHERE s.year=:inYear AND s.month=:inMonth ORDER by s.quantity desc";
        Query query = em.createQuery(strQuery);
        int year = Calendar.getInstance().get(Calendar.YEAR);
        query.setParameter("inYear", year);
        int month = Calendar.getInstance().get(Calendar.MONTH);
        query.setParameter("inMonth", month);

        List<Sales> list = (List<Sales>) query.getResultList();
        
        List<Wine> wines = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            Wine w = list.get(i).getWine();
            wines.add(w);
        }

        return wines;
    }

}
